package cn.seecoder.ai.controller;

import cn.seecoder.ai.model.vo.ModelVO;
import cn.seecoder.ai.model.vo.ResultVO;
import cn.seecoder.ai.service.ModelService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author DingXiaoyu
 * @date 2023/4/12 15:30
 */
@Slf4j
@RestController
@RequestMapping("${apiPrefix}/ml/models")
public class ModelController {

    @Autowired
    ModelService modelService;


    @ApiOperation("新建模型")
    @PostMapping("")
    public ResultVO<ModelVO> createModel(@RequestBody ModelVO modelVO){
        return ResultVO.buildSuccess(modelService.createModel(modelVO));
    }

    @ApiOperation("获取模型")
    @GetMapping("")
    public ResultVO<List<ModelVO>> getModelVOList(@RequestParam Integer userId){
        return ResultVO.buildSuccess(modelService.getModelVOList(userId));
    }

    @ApiOperation("获取某一配置下所有模型")
    @GetMapping("/configs/{configId}")
    public ResultVO<List<ModelVO>> getModelVOList(@RequestParam Integer userId,
                                                          @PathVariable Integer configId){
        return ResultVO.buildSuccess(modelService.getModelVOList(userId,configId));
    }

    @ApiOperation("获得模型详细信息")
    @GetMapping("/{modelId}")
    public ResultVO<ModelVO> getModelVO(@RequestParam Integer userId,
                                            @PathVariable Integer modelId){
        return ResultVO.buildSuccess(modelService.getModelVO(userId, modelId));
    }

    @ApiOperation("训练某个模型")
    @PostMapping("/{modelId}/train")
    public ResultVO<ModelVO> trainModel(@RequestParam Integer userId,
                                        @PathVariable Integer modelId){
        ModelVO modelVO = modelService.renewModel(userId, modelId);
        modelService.asyncTrainModel(userId, modelId);
        return ResultVO.buildSuccess(modelVO);
    }

}
